<?php
namespace Common\Model;

use Think\Model;

class InterestsModel extends Model
{

    protected $_validate = array(
        array(
            'name',
            'identicalNull',
            '',
            1,
            'callback'
        )
    );

    protected $_auto = array(
        array(
            'addtime',
            'time',
            1,
            'function'
        )
    ) // 添加时间
;

    public function add_interests($data)
    {
        if (false === $this->create($data)) {
            return array(
                'state' => 0,
                'error' => $this->getError()
            );
        } else {
            if (false === $insert_id = $this->add()) {
                return array(
                    'state' => 0,
                    'error' => '数据添加失败！'
                );
            }
        }
        
        return array(
            'state' => 1,
            'id' => $insert_id
        );
    }

    public function save_interests($data, $user)
    {
        if (false === $this->create($data)) {
            return array(
                'state' => 0,
                'error' => $this->getError()
            );
        } else {
            if (false === $this->save()) {
                return array(
                    'state' => 0,
                    'error' => '数据添加失败！'
                );
            }
        }
        
        return array(
            'state' => 1,
            'id' => $data['id']
        );
    }

    public function get_interests()
    {
		$interests = $this->field('id,name,class')->order('addtime desc')->select();
		
        return $interests;
    }
}
?>